package com.lc.pianostudent.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.adapter.ContactDetailAdapter;
import com.lc.pianostudent.adapter.HomeNewsAdapter;
import com.lc.pianostudent.bean.ContactDetailBean;
import com.lc.pianostudent.bean.GetStudyPictureBean;
import com.lc.pianostudent.bean.HomeNewsBean;
import com.lc.pianostudent.conn.PostMailGetstudy;
import com.lc.pianostudent.conn.PostMailXingka;
import com.lc.pianostudent.conn.PostUploadAvatar;
import com.lc.pianostudent.fragment.MineFragment;
import com.lc.pianostudent.view.ScrollLinearLayoutManager;
import com.lc.pianostudent.view.SelectPopupWindow;
import com.squareup.picasso.Picasso;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/2.
 */

public class ContactDetailActivity extends BaseActivity implements View.OnClickListener{

    private List<GetStudyPictureBean> contactDetailList = new ArrayList<>();


    @BoundView(R.id.contact_detail_recycle_view) private RecyclerView recyclerView;
    private ContactDetailAdapter contactDetailAdapter;

    @BoundView(R.id.name) private TextView name;
    @BoundView(R.id.person) private TextView person;
    @BoundView(R.id.wealth) private TextView wealth;
    @BoundView(R.id.fenshu) private TextView fenshu;
    @BoundView(R.id.nianji) private TextView nianji;
    @BoundView(R.id.jishu) private TextView jishu;
    @BoundView(R.id.jingyan) private TextView jingyan;
    @BoundView(R.id.xing_one) private ImageView xing_one;
    @BoundView(R.id.xing_two) private ImageView xing_two;
    @BoundView(R.id.xing_three) private ImageView xing_three;
    @BoundView(R.id.xing_four) private ImageView xing_four;
    @BoundView(R.id.xing_five) private ImageView xing_five;
    @BoundView(R.id.xing_six) private ImageView xing_six;
    @BoundView(R.id.xing_seven) private ImageView xing_seven;
    @BoundView(R.id.xing_eight) private ImageView xing_eight;
    @BoundView(R.id.xing_nine) private ImageView xing_nine;
    @BoundView(R.id.xing_ten) private ImageView xing_ten;
    @BoundView(R.id.xing_eleven) private ImageView xing_eleven;
    @BoundView(R.id.xing_twelve) private ImageView xing_twelve;
    @BoundView(R.id.xing_one_layout) private LinearLayout xing_one_layout;
    @BoundView(R.id.xing_two_layout) private LinearLayout xing_two_layout;
    @BoundView(value = R.id.xingka_main,isClick = true) private LinearLayout xingka_main;
    private PostMailGetstudy.PostMailGetstudyInfo currentInfo;
    @BoundView(R.id.hair) private ImageView hair;
    @BoundView(R.id.shoe) private ImageView shoe;
    @BoundView(R.id.jacket) private ImageView jacket;
    @BoundView(R.id.access) private ImageView access;
    @BoundView(R.id.suit) private ImageView suit;
    @BoundView(R.id.download) private ImageView download;
    @BoundView(R.id.maozi) private ImageView maozi;
    @BoundView(R.id.shoutao) private ImageView shoutao;
    @BoundView(R.id.wazi) private ImageView wazi;
    @BoundView(R.id.xianglian) private ImageView xianglian;
    @BoundView(R.id.erhuan) private ImageView erhuan;
    @BoundView(R.id.yanjing) private ImageView yanjing;
    @BoundView(R.id.mianshi) private ImageView mianshi;
    @BoundView(R.id.luomo) private ImageView luomo;
    @BoundView(R.id.biaoqing) private ImageView biaoqing;
    private SelectPopupWindow menuWindow;
    private String num = "";
    //星卡点击看大图
    public PostMailXingka postMailXingka = new PostMailXingka(new AsyCallBack<PostMailXingka.PostMailXingkaInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostMailXingka.PostMailXingkaInfo postMailXingkaInfo) throws Exception {
            menuWindow = new SelectPopupWindow(ContactDetailActivity.this,postMailXingkaInfo.imgList,0);
            //显示窗口 //设置layout在PopupWindow中显示的位置
            menuWindow.showAtLocation(xing_one_layout, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    public PostMailGetstudy postMailGetstudy = new PostMailGetstudy(new AsyCallBack<PostMailGetstudy.PostMailGetstudyInfo>(){

        @Override
        public void onSuccess(String toast, int type, Object o, PostMailGetstudy.PostMailGetstudyInfo postMailGetstudyInfo) throws Exception {
            name.setText(postMailGetstudyInfo.name+"( "+postMailGetstudyInfo.cname+" )");
            person.setText(postMailGetstudyInfo.person);
            wealth.setText(postMailGetstudyInfo.wealth);
            fenshu.setText(postMailGetstudyInfo.degree);
            nianji.setText("( "+postMailGetstudyInfo.xuewei+" )");
            jishu.setText(postMailGetstudyInfo.grade);
            jingyan.setText("( "+postMailGetstudyInfo.exper+" )");
            currentInfo = postMailGetstudyInfo;
            num = postMailGetstudyInfo.num;
            if(postMailGetstudyInfo.num.equals("0")){
                xing_one_layout.setVisibility(View.INVISIBLE);
                xing_two_layout.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("1")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_one.setVisibility(View.VISIBLE);
                xing_two.setVisibility(View.INVISIBLE);
                xing_three.setVisibility(View.INVISIBLE);
                xing_four.setVisibility(View.INVISIBLE);
                xing_five.setVisibility(View.INVISIBLE);
                xing_six.setVisibility(View.INVISIBLE);
                xing_two_layout.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("2")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_one.setVisibility(View.VISIBLE);
                xing_two.setVisibility(View.VISIBLE);
                xing_three.setVisibility(View.INVISIBLE);
                xing_four.setVisibility(View.INVISIBLE);
                xing_five.setVisibility(View.INVISIBLE);
                xing_six.setVisibility(View.INVISIBLE);
                xing_two_layout.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("3")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_one.setVisibility(View.VISIBLE);
                xing_two.setVisibility(View.VISIBLE);
                xing_three.setVisibility(View.VISIBLE);
                xing_four.setVisibility(View.INVISIBLE);
                xing_five.setVisibility(View.INVISIBLE);
                xing_six.setVisibility(View.INVISIBLE);
                xing_two_layout.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("4")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_one.setVisibility(View.VISIBLE);
                xing_two.setVisibility(View.VISIBLE);
                xing_three.setVisibility(View.VISIBLE);
                xing_four.setVisibility(View.VISIBLE);
                xing_five.setVisibility(View.INVISIBLE);
                xing_six.setVisibility(View.INVISIBLE);
                xing_two_layout.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("5")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_one.setVisibility(View.VISIBLE);
                xing_two.setVisibility(View.VISIBLE);
                xing_three.setVisibility(View.VISIBLE);
                xing_four.setVisibility(View.VISIBLE);
                xing_five.setVisibility(View.VISIBLE);
                xing_six.setVisibility(View.INVISIBLE);
                xing_two_layout.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("6")){
                xing_one_layout.setVisibility(View.VISIBLE);

                xing_two_layout.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("7")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_two_layout.setVisibility(View.VISIBLE);
                xing_seven.setVisibility(View.VISIBLE);
                xing_eight.setVisibility(View.INVISIBLE);
                xing_nine.setVisibility(View.INVISIBLE);
                xing_ten.setVisibility(View.INVISIBLE);
                xing_eleven.setVisibility(View.INVISIBLE);
                xing_twelve.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("8")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_two_layout.setVisibility(View.VISIBLE);
                xing_seven.setVisibility(View.VISIBLE);
                xing_eight.setVisibility(View.VISIBLE);
                xing_nine.setVisibility(View.INVISIBLE);
                xing_ten.setVisibility(View.INVISIBLE);
                xing_eleven.setVisibility(View.INVISIBLE);
                xing_twelve.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("9")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_two_layout.setVisibility(View.VISIBLE);
                xing_seven.setVisibility(View.VISIBLE);
                xing_eight.setVisibility(View.VISIBLE);
                xing_nine.setVisibility(View.VISIBLE);
                xing_ten.setVisibility(View.INVISIBLE);
                xing_eleven.setVisibility(View.INVISIBLE);
                xing_twelve.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("10")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_two_layout.setVisibility(View.VISIBLE);
                xing_seven.setVisibility(View.VISIBLE);
                xing_eight.setVisibility(View.VISIBLE);
                xing_nine.setVisibility(View.VISIBLE);
                xing_ten.setVisibility(View.VISIBLE);
                xing_eleven.setVisibility(View.INVISIBLE);
                xing_twelve.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("11")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_two_layout.setVisibility(View.VISIBLE);
                xing_seven.setVisibility(View.VISIBLE);
                xing_eight.setVisibility(View.VISIBLE);
                xing_nine.setVisibility(View.VISIBLE);
                xing_ten.setVisibility(View.VISIBLE);
                xing_eleven.setVisibility(View.VISIBLE);
                xing_twelve.setVisibility(View.INVISIBLE);
            }else if(postMailGetstudyInfo.num.equals("12")){
                xing_one_layout.setVisibility(View.VISIBLE);
                xing_two_layout.setVisibility(View.VISIBLE);

            }
            Picasso.with(context).load(postMailGetstudyInfo.mote).into(luomo);
            //头发
            if(postMailGetstudyInfo.hair_id.equals("")){
                hair.setVisibility(View.INVISIBLE);
            }else {
                hair.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.hair_picurl,hair,R.mipmap.new_head);
                Picasso.with(context).load(postMailGetstudyInfo.hair_picurl).into(hair);
            }
            //上身
            if(postMailGetstudyInfo.jack_id.equals("")){
                jacket.setVisibility(View.INVISIBLE);
            }else {
                jacket.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.jack_picurl,jacket);
                Picasso.with(context).load(postMailGetstudyInfo.jack_picurl).into(jacket);
            }
            //下装
            if(postMailGetstudyInfo.download_id.equals("")){
                download.setVisibility(View.INVISIBLE);
            }else {
                download.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.download_picurl,download);
                Picasso.with(context).load(postMailGetstudyInfo.download_picurl).into(download);

            }

            //表情
            if(postMailGetstudyInfo.expression_id.equals("")){
                biaoqing.setVisibility(View.INVISIBLE);
            }else {
                biaoqing.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.download_picurl,download);
                Picasso.with(context).load(postMailGetstudyInfo.expression_picurl).into(biaoqing);

            }
            //配饰
            if(postMailGetstudyInfo.accessShowList.size() == 0){
                access.setVisibility(View.INVISIBLE);
                maozi.setVisibility(View.INVISIBLE);
                erhuan.setVisibility(View.INVISIBLE);
                wazi.setVisibility(View.INVISIBLE);
                yanjing.setVisibility(View.INVISIBLE);
                shoutao.setVisibility(View.INVISIBLE);
                mianshi.setVisibility(View.INVISIBLE);
                xianglian.setVisibility(View.INVISIBLE);
            }else {
                for(int i = 0;i<postMailGetstudyInfo.accessShowList.size();i++){


                    if(postMailGetstudyInfo.accessShowList.get(i).getType().equals("14")){  //手套
                        shoutao.setVisibility(View.VISIBLE);

//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),shoutao);
                        Picasso.with(ContactDetailActivity.this).load(postMailGetstudyInfo.accessShowList.get(i).getPicurl()).into(shoutao);

                    }else
                    if(postMailGetstudyInfo.accessShowList.get(i).getType().equals("13")){  //包
                        access.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsCheckInfo.accessShowList.get(i).getPicurl(),access);
                        Picasso.with(ContactDetailActivity.this).load(postMailGetstudyInfo.accessShowList.get(i).getPicurl()).into(access);
                    }else
                    if(postMailGetstudyInfo.accessShowList.get(i).getType().equals("12")){  //耳环
                        erhuan.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),erhuan);
                        Picasso.with(ContactDetailActivity.this).load(postMailGetstudyInfo.accessShowList.get(i).getPicurl()).into(erhuan);

                    }else

                    if(postMailGetstudyInfo.accessShowList.get(i).getType().equals("16")){  //袜子
                        wazi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),wazi);
                        Picasso.with(ContactDetailActivity.this).load(postMailGetstudyInfo.accessShowList.get(i).getPicurl()).into(wazi);

                    }else
                    if(postMailGetstudyInfo.accessShowList.get(i).getType().equals("18")){  //眼睛
                        yanjing.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),yanjing);
                        Picasso.with(ContactDetailActivity.this).load(postMailGetstudyInfo.accessShowList.get(i).getPicurl()).into(yanjing);

                    }else
                    if(postMailGetstudyInfo.accessShowList.get(i).getType().equals("19")){  //帽子
                        maozi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),maozi);
                        Picasso.with(ContactDetailActivity.this).load(postMailGetstudyInfo.accessShowList.get(i).getPicurl()).into(maozi);

                    }else
                    if(postMailGetstudyInfo.accessShowList.get(i).getType().equals("20")){  //面饰
                        mianshi.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),mianshi);
                        Picasso.with(ContactDetailActivity.this).load(postMailGetstudyInfo.accessShowList.get(i).getPicurl()).into(mianshi);

                    } else
                    if(postMailGetstudyInfo.accessShowList.get(i).getType().equals("21")){  //项链
                        xianglian.setVisibility(View.VISIBLE);
//                        GlideLoader.getInstance().get(postGoodsIndexInfo.accessShowList.get(i).getPicurl(),xianglian);
                        Picasso.with(ContactDetailActivity.this).load(postMailGetstudyInfo.accessShowList.get(i).getPicurl()).into(xianglian);

                    }
                }


            }
            //鞋子
            if(postMailGetstudyInfo.shose_id.equals("")){
                shoe.setVisibility(View.INVISIBLE);
            }else {
                shoe.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.shose_picurl,shoe);
                Picasso.with(context).load(postMailGetstudyInfo.shose_picurl).into(shoe);

            }
            //套装
            if(postMailGetstudyInfo.suit_id.equals("")){
                suit.setVisibility(View.INVISIBLE);
            }else {
                suit.setVisibility(View.VISIBLE);
//                GlideLoader.getInstance().get(postGoodsCheckInfo.suit_picurl,suit);
                Picasso.with(context).load(postMailGetstudyInfo.suit_picurl).into(suit);

            }

            if(type == 0){
                contactDetailList.clear();
            }

            contactDetailList.addAll(postMailGetstudyInfo.contactDetailList);
            contactDetailAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detail);
        setBackTrue();
        setAppCallBack(new CallBack());
        iniView();
    }

    private void iniView(){
        setTitleName("详细资料");



        ScrollLinearLayoutManager scrollLinearLayoutManager = new ScrollLinearLayoutManager(this);
        recyclerView.setLayoutManager(scrollLinearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        contactDetailAdapter = new ContactDetailAdapter(this, contactDetailList);
        recyclerView.setAdapter(contactDetailAdapter);
        postMailGetstudy.id = getIntent().getStringExtra("id");
        postMailGetstudy.type = getIntent().getStringExtra("type");
        postMailGetstudy.execute();

        contactDetailAdapter.setOnItemClickListener(new ContactDetailAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                startActivity(new Intent(ContactDetailActivity.this,ContactDetailItemActivity.class)
                .putExtra("id",currentInfo.contactDetailList.get(position).getId()).putExtra("user_id",getIntent().getStringExtra("id")));
            }
        });
    }

    private Drawable loadImageFromNetwork(String imageUrl) {
        Drawable drawable = null;
        try {
            // 可以在这里通过第二个参数(文件名)来判断，是否本地有此图片
            drawable = Drawable.createFromStream(new URL(imageUrl).openStream(), null);
        } catch (IOException e) {
            Log.d("skythinking", e.getMessage());
        }
        if (drawable == null) {
            Log.d("skythinking", "null drawable");
        } else {
            Log.d("skythinking", "not null drawable");
        }

        return drawable;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.xingka_main:
                if(num.equals("0")){

                }else {
                    postMailXingka.num = num;
                    postMailXingka.execute(ContactDetailActivity.this, false);
                }
                break;
        }
    }

    public class CallBack implements AppCallBack {

        public void onRefresh() {
            postMailGetstudy.id = getIntent().getStringExtra("id");
            postMailGetstudy.type = getIntent().getStringExtra("type");
            postMailGetstudy.execute(ContactDetailActivity.this,false,0);

        }

    }
}
